# Maintainer: Oleg Tolmatcev <oleg.tolmatcev@gmail.com>

_realname=cvise
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=2.11.0
pkgrel=5
pkgdesc="Super-parallel Python port of the C-Reduce (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://github.com/marxin/cvise"
msys2_references=(
  "aur: cvise"
)
license=('spdx:BSD-3-Clause-Clear')
depends=(
  "${MINGW_PACKAGE_PREFIX}-gcc-libs"
  "${MINGW_PACKAGE_PREFIX}-clang-libs"
  "${MINGW_PACKAGE_PREFIX}-clang"
  "${MINGW_PACKAGE_PREFIX}-llvm-libs"
  "${MINGW_PACKAGE_PREFIX}-python"
  "${MINGW_PACKAGE_PREFIX}-python-chardet"
  "${MINGW_PACKAGE_PREFIX}-python-pebble"
  "${MINGW_PACKAGE_PREFIX}-python-psutil"
  "${MINGW_PACKAGE_PREFIX}-unifdef"
)
makedepends=(
  "${MINGW_PACKAGE_PREFIX}-cc"
  "${MINGW_PACKAGE_PREFIX}-cmake"
  "${MINGW_PACKAGE_PREFIX}-ninja"
  "${MINGW_PACKAGE_PREFIX}-clang-tools-extra"
)
checkdepends=("${MINGW_PACKAGE_PREFIX}-python-pytest")
optdepends=("colordiff")
source=("https://github.com/marxin/cvise/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz"
        "001-fix-build-with-llvm-20.patch::https://github.com/marxin/cvise/commit/29fc072f.patch"
        "002-make-relocatable.patch"
        "003-fix-cvise-delta.patch"
        "004-add-basic-bash-support.patch")
sha256sums=('7e3e473843aa79afb98f581d2e100efa47db80df3a961565b691d7b4a4ebd14b'
            '99271cc615968178fc6edefb3cd50656bf513b2e7a6294f104fa5536d0b8a6ae'
            'a0ecb4e04bc2315f62545c2b001ed4e9c8c4d74a5f31dfae3319ac05bc76e5c8'
            '2544e65b5100f72961066e314d0ee0ccded1241e2c48c0d85e8157c932a6966b'
            'f2ad4266b9d3d43c44320725f97f4b6f94172a77dd2e38917ada20ffc25dc637')

prepare() {
  cd ${_realname}-${pkgver}
  patch -p1 -i "${srcdir}"/001-fix-build-with-llvm-20.patch
  patch -p1 -i "${srcdir}"/002-make-relocatable.patch
  patch -p1 -i "${srcdir}"/003-fix-cvise-delta.patch
  patch -p1 -i "${srcdir}"/004-add-basic-bash-support.patch
}

build() {
  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  ${MINGW_PREFIX}/bin/cmake \
    -Wno-dev \
    -G Ninja \
    -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
    -DCMAKE_INSTALL_LIBEXECDIR=lib \
    -DPython3_EXECUTABLE=${MINGW_PREFIX}/bin/python \
    "${_extra_config[@]}" \
    -B build-${MSYSTEM} \
    -S "${_realname}-${pkgver}"

  ${MINGW_PREFIX}/bin/cmake --build build-${MSYSTEM}
}

check() {
    ${MINGW_PREFIX}/bin/ctest --test-dir build-${MSYSTEM} --output-on-failure
}

package() {
  ${MINGW_PREFIX}/bin/cmake --install build-${MSYSTEM} --prefix "${pkgdir}"${MINGW_PREFIX}

  ${MINGW_PREFIX}/bin/python -m compileall -o 0 -o 1 -q "${pkgdir}"${MINGW_PREFIX}/share/cvise

  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/COPYING \
    "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
}
